home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 644 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  4.6 KB

  1. Path: library.erc.clarkson.edu!rpi!not-for-mail
  2. From: kanze@gabi-soft.fr (J. Kanze)
  3. Newsgroups: comp.lang.c++.moderated,comp.lang.c++
  4. Subject: Re: Meaning of the specifier volatile?
  5. Followup-To: comp.lang.c++.moderated,comp.lang.c++
  6. Date: 5 Jan 1996 12:35:16 -0000
  7. Organization: GABI Software, Sarl.
  8. Sender: cppmods@netlab.cs.rpi.edu
  9. Approved: Dietmar.Kuehl@uni-konstanz.de
  10. Message-ID: <4cj5u4$qjm@netlab.cs.rpi.edu>
  11. References: <4c9740$27n@netlab.cs.rpi.edu> <4cff74$gfj@netlab.cs.rpi.edu>
  12. NNTP-Posting-Host: netlab.cs.rpi.edu
  13.  
  14. X-Original-Date: 5 Jan 1996 10:40:05 +0100
  15.  
  16. Robert C. Martin (rmartin@oma.com) wrote:
  17.  
  18. |> In article <4c9740$27n@netlab.cs.rpi.edu> Srinivas Vobilisetti
  19. |> <srv@cs.wayne.edu> writes:
  20.  
  21. |>    Nowhere i could find exact meaning of the specifier volatile.
  22.  
  23. |> Consider the following piece of code:
  24.  
  25. |> int n;
  26. |> for (int i=0; i<99; i++)
  27. |>   n=0;
  28.  
  29. |> A good optimising compiler should rewrite this as:
  30.  
  31. |> int n=0;
  32. |> int i=100;
  33.  
  34. |> {That's right, if you want to delay for 100 micro seconds, the above
  35. |> is not a portable approach.}
  36.  
  37. |> The use of the keyword volatile prevents the compiler from optimising
  38. |> the reads and writes to a variable.  
  39.  
  40. |> volatile int n;
  41. |> for (int i=0; i<99; i++)
  42. |>   n=0;
  43.  
  44. |> Will cause zero to be written into the variable n 100 times. 
  45. |> {Note, the compiler may be smart enough to do this without using the
  46. |> variable i!}.
  47.  
  48. |> volatile int n;
  49. |> for (volatile int i=0; i<99; i++)
  50. |>   n=0;
  51.  
  52. |> Will cause zero to be written into the variable n 100 times.  It will
  53. |> also cause the variable i to be incremented inbetween writes to n, and
  54. |> will read i to test it for 99 each time through the loop.
  55.  
  56. Note, of course, that the exact meaning of operator ++ on a volatile
  57. object is somewhat open to discussion.  For example, on a machine with a
  58. hardware increment instruction, the compiler might increment `i' in
  59. memory, *THEN* read it into a register, decrement the value and use it.
  60. This may result in the value being read twice.
  61.  
  62. If the object in question is a normal memory object, and you are not
  63. concerned with asynchronous events (signals, asynchronous thread
  64. changes), this is probably not a problem.  If the object in question is
  65. a memory mapped IO port, where a read has a side effect, this may be a
  66. disaster.
  67.  
  68. As a general rule, the only things I would try to do with a volatile
  69. object are to read them and write them directly, and not in the same
  70. expression.  If I really had to increment a volatile int, I would
  71. generally use three statements: tmp = i ; tmp ++ ; i = tmp ;.  (Of
  72. course, since anything like this will be unportable by definition, I
  73. would read the compiler documentation to see exactly how the compiler
  74. treated volatile.)
  75.  
  76. |> Generally, this is used when certain memory registers have side
  77. |> effects.  e.g.
  78.  
  79. |> int * volatile tty = 0x100;  // 0x100 is the addr of the tty device.
  80.                         ^^^^^
  81.  
  82. This shouldn't work without an explicit cast, should it?
  83.  
  84. |> *tty = 'h'; // prints a 'c' on the tty;
  85.  
  86. |> void PrintMessage(char *s)
  87. |> {
  88. |>   for(; *s; s++) *tty = *s; // prints the string on the tty.
  89. |> }
  90.  
  91. |> You can also use volatile if you want to create stupid delays as in
  92. |> the first example.
  93.  
  94. Except that the length of the delay would still be undefined, since the
  95. compiler can insert any additional code it likes into the loop.
  96.  
  97. On point concerning volatile that I have not seen made is the fact that
  98. the C/C++ concept of volatile is orthogonal to atomicity of access.
  99. Declaring an object volatile is *NOT* sufficient with regards to
  100. asynchronous events; the object must still be of a type for which the
  101. compiler will generate atomic accesses.  In general, char, short, and
  102. int are safe, although there are exceptions.  (On 8 bit machines,
  103. generally only char will be safe, and on the TI 9900 (and probably some
  104. word addressed machines), int will be safe, but not char.)
  105.  
  106. In portable code, all you can count on is that at least one integral
  107. type will be safe; that type is defined by a typedef to sig_atomic_t in
  108. signal.h.  So in practice, there is almost nothing portable that you can
  109. do in the presence of asynchronous events.
  110. -- 
  111. James Kanze           (+33) 88 14 49 00          email: kanze@gabi-soft.fr
  112. GABI Software, Sarl., 8 rue des Francs Bourgeois, 67000 Strasbourg, France
  113. Conseils, itudes et rialisations en logiciel orienti objet --
  114.               -- A la recherche d'une activiti dans une region francophone
  115.  
  116.     [ comp.lang.c++.moderated is a moderated newsgroup.  Submit articles ]
  117.     [  to <c++-submit@netlab.cs.rpi.edu>.  The moderation policy can be  ]
  118.     [   retrieved from <http://netlab.cs.rpi.edu/~cppmods/guide.html>.   ]
  119.     [    Moderators can be reached at: c++-request@netlab.cs.rpi.edu.    ]
  120.